import Vue from 'vue'
import Router from 'vue-router'
import global from '../assets/js/global_variable';
import Home from '@/components/home';
import Login from '@/components/login';
import Buy from '@/components/buy';
import Idcard from '@/components/idcard';
import ContractList from '@/components/crontractList';
import Canvas from '@/components/canvas';
import Error from '@/components/error';
import WxPay from '@/components/wxPay';
import Compliance from '@/components/compliance';
import Result from '@/components/result';
import LoginWeb from '@/components/loginWeb';
import Loading from '@/components/loading';
import AlipayResult from '@/components/alipayResult';
import ShortLine from '@/components/shortLine';

Vue.use(Router);

const router =  new Router({
    routes: [
        {name: 'default', path: '', redirect: {name: 'login'}, meta: {title: '短线一日游'}}, // 默认
        {name: 'login', path: '/login', component: Login, meta: {title: '登录'}}, // 登录 /:channelId/
        {name: 'home', path: '/home', component: Home, meta: {title: '短线一日游'}}, // 首页
        {name: 'buy', path: '/buy', component: Buy, meta: {title: '订单详情'}}, // 购买页面
        {name: 'wxPay', path: '/wxPay', component: WxPay, meta: {title: '微信扫码支付'}},
        {name: 'idcard', path: '/idcard', component: Idcard, meta: {title: '实名验证'}},
        {name: 'contractList', path: '/contractList', component: ContractList, meta: {title: '合同列表'}},
        {name: 'canvas', path: '/canvas', component: Canvas, meta: {title: '签字'}},
        {name: 'error', path: '/error', component: Error ,meta: {title: '404'}},
        {name: 'compliance', path: '/compliance', component: Compliance, meta: {title: '合规问卷'}},
        {name: 'result', path: '/result', component: Result},
        {name: 'loginWeb', path: '/loginWeb', component: LoginWeb, meta: {title: '登录'}},//:channelId/
        {name: 'loading', path: '/loading', component: Loading ,meta: {title: '微信扫码登录'}},
        {name: 'alipayResult', path: '/alipayResult', component: AlipayResult ,meta: {title: '支付宝支付'}},
        {name: 'shortLine', path: '/shortLine', component: ShortLine ,meta: {title: '短线操作'}},
    ]
});
router.beforeEach((to ,from ,next)=>{
    if (to.meta.title) {
        document.title = to.meta.title
    }
    if(to.meta.needLogin){
        if(store.state.isLogin){
            next();
        }else{
            next({
                path : '/login' ,
                query : {redirect: to.fullPath}
            })
        }
    }else{
        if(to.name =='login' && global.isMobile== null){
            next({
                path : '/loginWeb' ,
                query : {
                    channelId : to.query.channelId
                }
            });
        }else{
            next();
        }
    }
});
export  default  router;
